//
//  SearchViewController.m
//  KiklinkApp
//
//  Created by LJC on 14-9-24.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "SearchViewController.h"

#import "ImagesManager.h"
#import "UIView+loadWithNib.h"
#import "UIColor+hex.h"

#import "SearchBar.h"
#import "LocalCell.h"
#import "MJRefresh.h"

#import "ProductViewController.h"

#import "API.h"

static NSString *SearchTableCellIdentifier1 = @"LocalCell";
static NSString *SearchTableCellIdentifier2 = @"LocalCell";
static NSString *SearchTableCellIdentifier3 = @"LocalCell";

const int Interface_Loading = 1000;
const int Interface_Loaded = 2000;
const int Interface_Failed = 3000;
const int Interface_NoSearchResult = 4000;

@interface SearchViewController ()
{
    //data
    NSMutableArray *searchTableArray1;
    NSMutableArray *searchTableArray2;
    NSMutableArray *searchTableArray3;
    NSInteger page1;
    NSInteger page2;
    NSInteger page3;
    NSInteger totalPage1;
    NSInteger totalPage2;
    NSInteger totalPage3;
    
    //ui
    UIBarButtonItem *searchItem;
    SearchBar *search;
    UIBarButtonItem *searchBarItem;
    UIButton *backButton;
}
@end

@implementation SearchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"搜索结果";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    
    
    //loading
//    [self.activity1 startAnimating];
//    [self.activity2 startAnimating];
//    [self.activity3 startAnimating];
//    self.refreshBtn1.hidden = YES;
//    self.refreshBtn2.hidden = YES;
//    self.refreshBtn3.hidden = YES;
    
    [self interfaceSwitcher:Interface_Loading tableNum:1 isFilled:NO];
    [self interfaceSwitcher:Interface_Loading tableNum:2 isFilled:NO];
    [self interfaceSwitcher:Interface_Loading tableNum:3 isFilled:NO];
    
    [self.refreshBtn1 addTarget:self action:@selector(refresh1) forControlEvents:UIControlEventTouchUpInside];
    [self.refreshBtn2 addTarget:self action:@selector(refresh2) forControlEvents:UIControlEventTouchUpInside];
    [self.refreshBtn3 addTarget:self action:@selector(refresh3) forControlEvents:UIControlEventTouchUpInside];
    
    
    //BundleImages
    ImagesManager *imagesManager = [ImagesManager sharedImagesManager];
    
    
    //leftNavItem
    backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, NavBarHeight/4, NavBarHeight/2)];
    [backButton setBackgroundImage:[imagesManager back] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[imagesManager backTapped] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //rightNavItem
    searchItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchBarPresent)];
    searchItem.tintColor = [UIColor colorWithHexString:AppColor_Main andAlpha:0.8];
    
    search = [[SearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth*9/10, NavBarHeight)];
    search.placeholder = @"向右滑动取消";
    search.delegate = self;
    UISwipeGestureRecognizer *rightSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(searchBarDismis)];
    rightSwip.direction = UISwipeGestureRecognizerDirectionRight;
    [search addGestureRecognizer:rightSwip];
    searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:search];
    
    self.navigationItem.rightBarButtonItem = searchItem;
    
    
    //segment
    self.segment.selectedSegmentIndex = 0;
    self.segment.tintColor = [UIColor colorWithHexString:AppColor_Main];
    [self.segment addTarget:self action:@selector(segmentAction) forControlEvents:UIControlEventValueChanged];
    
    
    //localTable
    [self.searchTable2 setSeparatorInset:UIEdgeInsetsZero];
    self.searchTable2.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.searchTable2.separatorColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.searchTable2.delegate = self;
    self.searchTable2.dataSource = self;
    [self.searchTable2 registerClass:[UITableViewCell class] forCellReuseIdentifier:SearchTableCellIdentifier2];
    [self.searchTable2 reloadData];
    
    [self.searchTable1 setSeparatorInset:UIEdgeInsetsZero];
    self.searchTable1.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.searchTable1.separatorColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.searchTable1.delegate = self;
    self.searchTable1.dataSource = self;
    [self.searchTable1 registerClass:[UITableViewCell class] forCellReuseIdentifier:SearchTableCellIdentifier1];
    [self.searchTable1 reloadData];
    
    [self.searchTable3 setSeparatorInset:UIEdgeInsetsZero];
    self.searchTable3.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.searchTable3.separatorColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.searchTable3.delegate = self;
    self.searchTable3.dataSource = self;
    [self.searchTable3 registerClass:[UITableViewCell class] forCellReuseIdentifier:SearchTableCellIdentifier3];
    [self.searchTable3 reloadData];
    
    [self setupRefresh];
    
    //data
    searchTableArray1 = [[NSMutableArray alloc] init];
    searchTableArray2 = [[NSMutableArray alloc] init];
    searchTableArray3 = [[NSMutableArray alloc] init];
    page1 = 1;
    page2 = 1;
    page3 = 1;
    [self getData:1 Condition:@"first" SortStyle:@""];
    [self getData:1 Condition:@"first" SortStyle:@"price"];
    [self getData:1 Condition:@"first" SortStyle:@"buyedCount desc"];
}

#pragma mark - get data
//condition:first&footer&reGet  sortStyle:price&buyedCount desc
- (void)getData:(NSInteger)pageNum Condition:(NSString *)condition SortStyle:(NSString *)sortStyle
{
    NSLog(@"page:%ld",(long)pageNum);
    
    [API courseSearchWithKeyword:self.keyWord distance:@0 tags:@"" category:@0 sortStyle:sortStyle pageNumber:[NSNumber numberWithInteger:pageNum] Callback:^(int stateCode, NSString *word, id content) {
            
        if (stateCode == SUCCESS) {
            ProductPage *productPage = (ProductPage *)content;
            NSMutableArray *result = [[NSMutableArray alloc] initWithArray:productPage.result];
            
            if (productPage.totalCount != 0) {
                
                if ([sortStyle isEqualToString:@""]) {
                    
                    if ([condition isEqualToString:@"first"]) {
                        totalPage1 = productPage.lastPageNumber;
                        //                    self.searchTable1.scrollEnabled = YES;
                        page1 = 2;
                        searchTableArray1 = result;
                    } else if ([condition isEqualToString:@"footer"]) {
                        page1 ++;
                        [searchTableArray1 addObjectsFromArray:result];
                    } else if ([condition isEqualToString:@"reGet"]) {
                        totalPage1 = productPage.lastPageNumber;
                        //                    self.searchTable1.scrollEnabled = YES;
                        page1 = 2;
                        searchTableArray1 = result;
                    }
                    
                    [self loadData1:condition];
                    
                } else if ([sortStyle isEqualToString:@"price"]) {
                    
                    if ([condition isEqualToString:@"first"]) {
                        totalPage2 = productPage.lastPageNumber;
                        //                    self.searchTable2.scrollEnabled = YES;
                        page2 = 2;
                        searchTableArray2 = result;
                    } else if ([condition isEqualToString:@"footer"]) {
                        page2 ++;
                        [searchTableArray2 addObjectsFromArray:result];
                    } else if ([condition isEqualToString:@"reGet"]) {
                        totalPage2 = productPage.lastPageNumber;
                        //                    self.searchTable2.scrollEnabled = YES;
                        page2 = 2;
                        searchTableArray2 = result;
                    }
                    
                    [self loadData2:condition];
                    
                } else if ([sortStyle isEqualToString:@"buyedCount desc"]) {
                    
                    if ([condition isEqualToString:@"first"]) {
                        totalPage3 = productPage.lastPageNumber;
                        //                    self.searchTable3.scrollEnabled = YES;
                        page3 = 2;
                        searchTableArray3 = result;
                    } else if ([condition isEqualToString:@"footer"]) {
                        page3 ++;
                        [searchTableArray3 addObjectsFromArray:result];
                    } else if ([condition isEqualToString:@"reGet"]) {
                        totalPage3 = productPage.lastPageNumber;
                        //                    self.searchTable3.scrollEnabled = YES;
                        page3 = 2;
                        searchTableArray3 = result;
                    }
                    
                    [self loadData3:condition];
                    
                }
                
            } else {
                
                if ([sortStyle isEqualToString:@""]) {
                    
                    if ([condition isEqualToString:@"first"]) {
                        page1 = 2;
                        searchTableArray1 = result;
                    } else if ([condition isEqualToString:@"footer"]) {
                        page1 ++;
                        [searchTableArray1 addObjectsFromArray:result];
                    } else if ([condition isEqualToString:@"reGet"]) {
                        page1 = 2;
                        searchTableArray1 = result;
                    }
                    
                    [self.searchTable1 reloadData];
                    [self interfaceSwitcher:Interface_NoSearchResult tableNum:1 isFilled:NO];
                    
                } else if ([sortStyle isEqualToString:@"price"]) {
                    
                    if ([condition isEqualToString:@"first"]) {
                        page2 = 2;
                        searchTableArray2 = result;
                    } else if ([condition isEqualToString:@"footer"]) {
                        page2 ++;
                        [searchTableArray2 addObjectsFromArray:result];
                    } else if ([condition isEqualToString:@"reGet"]) {
                        page2 = 2;
                        searchTableArray2 = result;
                    }
                    
                    [self.searchTable2 reloadData];
                    [self interfaceSwitcher:Interface_NoSearchResult tableNum:2 isFilled:NO];
                    
                } else if ([sortStyle isEqualToString:@"buyedCount desc"]) {
                    
                    if ([condition isEqualToString:@"first"]) {
                        page3 = 2;
                        searchTableArray3 = result;
                    } else if ([condition isEqualToString:@"footer"]) {
                        page3 ++;
                        [searchTableArray3 addObjectsFromArray:result];
                    } else if ([condition isEqualToString:@"reGet"]) {
                        page3 = 2;
                        searchTableArray3 = result;
                    }
                    
                    [self.searchTable3 reloadData];
                    [self interfaceSwitcher:Interface_NoSearchResult tableNum:3 isFilled:NO];
                    
                }
                
            }
            
        } else {
            
            if ([sortStyle isEqualToString:@""]) {
                
                if ([condition isEqualToString:@"first"]) {
//                    self.searchTable1.scrollEnabled = NO;
//                    [self.activity1 stopAnimating];
//                    self.refreshBtn1.hidden = NO;
                    [self interfaceSwitcher:Interface_Failed tableNum:1 isFilled:NO];
                } else if ([condition isEqualToString:@"footer"]) {
//                    [self.searchTable1 footerEndRefreshing];
                    [self interfaceSwitcher:Interface_Failed tableNum:1 isFilled:YES];
                } else if ([condition isEqualToString:@"reGet"]) {
//                    self.searchTable1.scrollEnabled = NO;
//                    [self.activity1 stopAnimating];
//                    self.refreshBtn1.hidden = NO;
                    [self interfaceSwitcher:Interface_Failed tableNum:1 isFilled:NO];
                }
                
            } else if ([sortStyle isEqualToString:@"price"]) {
                
                if ([condition isEqualToString:@"first"]) {
//                    self.searchTable2.scrollEnabled = NO;
//                    [self.activity2 stopAnimating];
//                    self.refreshBtn2.hidden = NO;
                    [self interfaceSwitcher:Interface_Failed tableNum:2 isFilled:NO];
                } else if ([condition isEqualToString:@"footer"]) {
//                    [self.searchTable2 footerEndRefreshing];
                    [self interfaceSwitcher:Interface_Failed tableNum:2 isFilled:YES];
                } else if ([condition isEqualToString:@"reGet"]) {
//                    self.searchTable2.scrollEnabled = NO;
//                    [self.activity2 stopAnimating];
//                    self.refreshBtn2.hidden = NO;
                    [self interfaceSwitcher:Interface_Failed tableNum:2 isFilled:NO];
                }
                
            } else if ([sortStyle isEqualToString:@"buyedCount desc"]) {
                
                if ([condition isEqualToString:@"first"]) {
//                    self.searchTable3.scrollEnabled = NO;
//                    [self.activity3 stopAnimating];
//                    self.refreshBtn3.hidden = NO;
                    [self interfaceSwitcher:Interface_Failed tableNum:3 isFilled:NO];
                } else if ([condition isEqualToString:@"footer"]) {
//                    [self.searchTable3 footerEndRefreshing];
                    [self interfaceSwitcher:Interface_Failed tableNum:3 isFilled:YES];
                } else if ([condition isEqualToString:@"reGet"]) {
//                    self.searchTable3.scrollEnabled = NO;
//                    [self.activity3 stopAnimating];
//                    self.refreshBtn3.hidden = NO;
                    [self interfaceSwitcher:Interface_Failed tableNum:3 isFilled:NO];
                }
                
            }
            
        }
        
    }];
}

- (void)loadData1:(NSString *)condition
{
    [self.searchTable1 reloadData];
    
    if ([condition isEqualToString:@"first"]) {
//        [self.activity1 stopAnimating];
        [self interfaceSwitcher:Interface_Loaded tableNum:1 isFilled:NO];
    } else if ([condition isEqualToString:@"footer"]) {
//        [self.searchTable1 footerEndRefreshing];
        [self interfaceSwitcher:Interface_Loaded tableNum:1 isFilled:YES];
    } else if ([condition isEqualToString:@"reGet"]) {
//        [self.refreshBtn1 setHidden:YES];
//        [self.activity1 stopAnimating];
        [self interfaceSwitcher:Interface_Loaded tableNum:1 isFilled:NO];
    }
}
- (void)loadData2:(NSString *)condition
{
    [self.searchTable2 reloadData];

    if ([condition isEqualToString:@"first"]) {
//        [self.activity2 stopAnimating];
        [self interfaceSwitcher:Interface_Loaded tableNum:2 isFilled:NO];
    } else if ([condition isEqualToString:@"footer"]) {
//        [self.searchTable2 footerEndRefreshing];
        [self interfaceSwitcher:Interface_Loaded tableNum:2 isFilled:YES];
    } else if ([condition isEqualToString:@"reGet"]) {
//        [self.refreshBtn2 setHidden:YES];
//        [self.activity2 stopAnimating];
        [self interfaceSwitcher:Interface_Loaded tableNum:2 isFilled:NO];
    }
}
- (void)loadData3:(NSString *)condition
{
    [self.searchTable3 reloadData];

    if ([condition isEqualToString:@"first"]) {
//        [self.activity3 stopAnimating];
        [self interfaceSwitcher:Interface_Loaded tableNum:3 isFilled:NO];
    } else if ([condition isEqualToString:@"footer"]) {
//        [self.searchTable3 footerEndRefreshing];
        [self interfaceSwitcher:Interface_Loaded tableNum:3 isFilled:YES];
    } else if ([condition isEqualToString:@"reGet"]) {
//        [self.refreshBtn3 setHidden:YES];
//        [self.activity3 stopAnimating];
        [self interfaceSwitcher:Interface_Loaded tableNum:3 isFilled:NO];
    }
}

- (void)refresh1
{
//    self.refreshBtn1.hidden = YES;
//    [self.activity1 startAnimating];
    
    [self interfaceSwitcher:Interface_Loading tableNum:1 isFilled:NO];
    
    [self getData:1 Condition:@"first" SortStyle:@""];
}
- (void)refresh2
{
//    self.refreshBtn2.hidden = YES;
//    [self.activity2 startAnimating];
    
    [self interfaceSwitcher:Interface_Loading tableNum:2 isFilled:NO];
    
    [self getData:1 Condition:@"first" SortStyle:@"price"];
}
- (void)refresh3
{
//    self.refreshBtn3.hidden = YES;
//    [self.activity3 startAnimating];
    
    [self interfaceSwitcher:Interface_Loading tableNum:3 isFilled:NO];
    
    [self getData:1 Condition:@"first" SortStyle:@"buyedCount desc"];
}
#pragma mark - get data End

#pragma mark - interfaceSwitcher
- (void)interfaceSwitcher:(int)interfaceStatus tableNum:(int)tableNum isFilled:(BOOL)isFilled
{
    switch (interfaceStatus) {
        case Interface_Loading:
            
            switch (tableNum) {
                case 1:
                    [self.activity1 startAnimating];
                    self.refreshBtn1.hidden = YES;
                    
                    self.searchTable1.scrollEnabled = NO;
                    [self.searchTable1 footerEndRefreshing];
                    self.searchTable1.hidden = YES;
                    self.noSearchResult1.hidden = YES;
                    
                    break;
                case 2:
                    [self.activity2 startAnimating];
                    self.refreshBtn2.hidden = YES;
                    
                    self.searchTable2.scrollEnabled = NO;
                    [self.searchTable2 footerEndRefreshing];
                    self.searchTable2.hidden = YES;
                    self.noSearchResult2.hidden = YES;
                    
                    break;
                case 3:
                    [self.activity3 startAnimating];
                    self.refreshBtn3.hidden = YES;
                    
                    self.searchTable3.scrollEnabled = NO;
                    [self.searchTable3 footerEndRefreshing];
                    self.searchTable3.hidden = YES;
                    self.noSearchResult3.hidden = YES;
                    
                    break;
                default:
                    break;
            }
            
            break;
        case Interface_Loaded:
            
            switch (tableNum) {
                case 1:
                    self.searchTable1.scrollEnabled = YES;
                    [self.activity1 stopAnimating];
                    [self.refreshBtn1 setHidden:YES];
                    [self.searchTable1 footerEndRefreshing];
                    
                    self.searchTable1.hidden = NO;
                    self.noSearchResult1.hidden = YES;
                    
                    break;
                case 2:
                    self.searchTable2.scrollEnabled = YES;
                    [self.activity2 stopAnimating];
                    [self.refreshBtn2 setHidden:YES];
                    [self.searchTable2 footerEndRefreshing];

                    self.searchTable2.hidden = NO;
                    self.noSearchResult2.hidden = YES;
                    
                    break;
                case 3:
                    self.searchTable3.scrollEnabled = YES;
                    [self.activity3 stopAnimating];
                    [self.refreshBtn3 setHidden:YES];
                    [self.searchTable3 footerEndRefreshing];

                    self.searchTable3.hidden = NO;
                    self.noSearchResult3.hidden = YES;
                    
                    break;
                default:
                    break;
            }
            
            break;
        case Interface_Failed:
            
            switch (tableNum) {
                case 1:
                    
                    if (isFilled) {
                        [self.searchTable1 footerEndRefreshing];
                        
                        self.searchTable1.scrollEnabled = YES;
                        [self.activity1 stopAnimating];
                        [self.refreshBtn1 setHidden:YES];
                        self.searchTable1.hidden = NO;
                        self.noSearchResult1.hidden = YES;
                        
                    } else {
                        self.searchTable1.scrollEnabled = NO;
                        [self.activity1 stopAnimating];
                        self.refreshBtn1.hidden = NO;
                        
                        [self.searchTable1 footerEndRefreshing];
                        self.searchTable1.hidden = YES;
                        self.noSearchResult1.hidden = YES;
                    }
                    
                    break;
                case 2:
                    
                    if (isFilled) {
                        [self.searchTable2 footerEndRefreshing];
                        
                        self.searchTable2.scrollEnabled = YES;
                        [self.activity2 stopAnimating];
                        [self.refreshBtn2 setHidden:YES];
                        self.searchTable2.hidden = NO;
                        self.noSearchResult2.hidden = YES;
                        
                    } else {
                        self.searchTable2.scrollEnabled = NO;
                        [self.activity2 stopAnimating];
                        self.refreshBtn2.hidden = NO;
                        
                        [self.searchTable2 footerEndRefreshing];
                        self.searchTable2.hidden = YES;
                        self.noSearchResult2.hidden = YES;
                        
                    }
                    
                    break;
                case 3:
                    
                    if (isFilled) {
                        [self.searchTable3 footerEndRefreshing];
                        
                        self.searchTable3.scrollEnabled = YES;
                        [self.activity3 stopAnimating];
                        [self.refreshBtn3 setHidden:YES];
                        self.searchTable3.hidden = NO;
                        self.noSearchResult3.hidden = YES;
                        
                    } else {
                        self.searchTable3.scrollEnabled = NO;
                        [self.activity3 stopAnimating];
                        self.refreshBtn3.hidden = NO;
                        
                        [self.searchTable3 footerEndRefreshing];
                        self.searchTable3.hidden = YES;
                        self.noSearchResult3.hidden = YES;
                    }
                    
                    break;
                default:
                    break;
            }
            
            break;
        case Interface_NoSearchResult:
            
            switch (tableNum) {
                case 1:
                    
                    self.searchTable1.scrollEnabled = NO;
                    [self.activity1 stopAnimating];
                    [self.refreshBtn1 setHidden:YES];
                    [self.searchTable1 footerEndRefreshing];
                    self.searchTable1.hidden = NO;
                    self.noSearchResult1.hidden = NO;
                    
                    break;
                case 2:
                    
                    self.searchTable2.scrollEnabled = NO;
                    [self.activity2 stopAnimating];
                    [self.refreshBtn2 setHidden:YES];
                    [self.searchTable2 footerEndRefreshing];
                    self.searchTable2.hidden = NO;
                    self.noSearchResult2.hidden = NO;
                    
                    break;
                case 3:
                    
                    self.searchTable3.scrollEnabled = NO;
                    [self.activity3 stopAnimating];
                    [self.refreshBtn3 setHidden:YES];
                    [self.searchTable3 footerEndRefreshing];
                    self.searchTable3.hidden = NO;
                    self.noSearchResult3.hidden = NO;
                    
                    break;
                default:
                    break;
            }
            
            break;
        default:
            break;
    }
}
#pragma mark - interfaceSwitcher End

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    
    self.title = @"搜索结果";
    [backButton setHidden:NO];
    [search resignFirstResponder];
    self.navigationItem.rightBarButtonItem = searchItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - back
- (void)back
{
    self.searchTable1.delegate = nil;
    self.searchTable2.delegate = nil;
    self.searchTable3.delegate = nil;
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - back End

#pragma mark - searchBarPresent&dismiss&input
- (void)searchBarPresent
{
    CATransition *animation = [CATransition animation];
    animation.duration = 0.3;
    animation.type = @"push";
    animation.subtype = @"fromRight";
    
    self.title = @"";
    [backButton setHidden:YES];
    self.navigationItem.rightBarButtonItem = searchBarItem;
    
    [self.navigationController.navigationBar.layer addAnimation:animation forKey:nil];
    [search becomeFirstResponder];
}
- (void)searchBarDismis
{
    CATransition *animation = [CATransition animation];
    animation.duration = 0.4;
    animation.type = @"push";
    animation.subtype = @"fromLeft";
    
    self.title = @"搜索结果";
    [backButton setHidden:NO];
    [search resignFirstResponder];
    self.navigationItem.rightBarButtonItem = searchItem;
    
    [self.navigationController.navigationBar.layer addAnimation:animation forKey:nil];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.keyWord = searchBar.text;
    page1 = 1;
    page2 = 1;
    page3 = 1;
    [self getData:1 Condition:@"reGet" SortStyle:@""];
    [self getData:1 Condition:@"reGet" SortStyle:@"price"];
    [self getData:1 Condition:@"reGet" SortStyle:@"buyedCount desc"];
    
    self.title = @"搜索结果";
    [backButton setHidden:NO];
    [search resignFirstResponder];
    self.navigationItem.rightBarButtonItem = searchItem;
    
    [self.searchTable1 setContentOffset:CGPointMake(0, 0)];
    [self.searchTable2 setContentOffset:CGPointMake(0, 0)];
    [self.searchTable3 setContentOffset:CGPointMake(0, 0)];
}
#pragma mark - searchBarPresent&dismiss&input End

#pragma mark - segmentAction
- (void)segmentAction
{
    NSInteger selectedIndex = self.segment.selectedSegmentIndex;
    switch (selectedIndex) {
        case 0:
//            [self.searchTable1 setHidden:NO];
//            [self.searchTable2 setHidden:YES];
//            [self.searchTable3 setHidden:YES];
            
            [self.loadingView1 setHidden:NO];
            [self.loadingView2 setHidden:YES];
            [self.loadingView3 setHidden:YES];
            break;
        case 1:
//            [self.searchTable1 setHidden:YES];
//            [self.searchTable2 setHidden:NO];
//            [self.searchTable3 setHidden:YES];
            
            [self.loadingView1 setHidden:YES];
            [self.loadingView2 setHidden:NO];
            [self.loadingView3 setHidden:YES];
            break;
        case 2:
//            [self.searchTable1 setHidden:YES];
//            [self.searchTable2 setHidden:YES];
//            [self.searchTable3 setHidden:NO];
            
            [self.loadingView1 setHidden:YES];
            [self.loadingView2 setHidden:YES];
            [self.loadingView3 setHidden:NO];
            break;
        default:
            break;
    }
}
#pragma mark - segmentAction End

#pragma tableView dataSource&delegate  &MJRefresh
- (void)setupRefresh
{
    [self.searchTable1 addFooterWithTarget:self action:@selector(footerRereshing)];
    self.searchTable1.footerPullToRefreshText = @"上拉加载更多";
    self.searchTable1.footerReleaseToRefreshText = @"松开马上加载";
    self.searchTable1.footerRefreshingText = @"正在加载...";
    
    [self.searchTable2 addFooterWithTarget:self action:@selector(footerRereshing)];
    self.searchTable2.footerPullToRefreshText = @"上拉加载更多";
    self.searchTable2.footerReleaseToRefreshText = @"松开马上加载";
    self.searchTable2.footerRefreshingText = @"正在加载...";
    
    [self.searchTable3 addFooterWithTarget:self action:@selector(footerRereshing)];
    self.searchTable3.footerPullToRefreshText = @"上拉加载更多";
    self.searchTable3.footerReleaseToRefreshText = @"松开马上加载";
    self.searchTable3.footerRefreshingText = @"正在加载...";
}
- (void)footerRereshing
{
    //data
    if (self.segment.selectedSegmentIndex == 0) {
        if (page1 <= totalPage1) {
            [self getData:page1 Condition:@"footer" SortStyle:@""];
        } else {
            [self.searchTable1 footerEndRefreshing];
        }
    } else if (self.segment.selectedSegmentIndex == 1) {
        if (page2 <= totalPage2) {
            [self getData:page2 Condition:@"footer" SortStyle:@"price"];
        } else {
            [self.searchTable2 footerEndRefreshing];
        }
    } else if (self.segment.selectedSegmentIndex == 2) {
        if (page3 <= totalPage3) {
            [self getData:page3 Condition:@"footer" SortStyle:@"buyedCount desc"];
        } else {
            [self.searchTable3 footerEndRefreshing];
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchTable1) {
        return searchTableArray1.count;
    } else if (tableView == self.searchTable2) {
        return searchTableArray2.count;
    } else if (tableView == self.searchTable3) {
        return searchTableArray3.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchTable1) {
        BOOL nibsRegistered = NO;
        if (!nibsRegistered) {
            UINib *nib = [UINib nibWithNibName:@"LocalCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:SearchTableCellIdentifier1];
            nibsRegistered = YES;
        }
        LocalCell *cell = (LocalCell *)[tableView dequeueReusableCellWithIdentifier:SearchTableCellIdentifier1];
        [cell loadDataWithProduct:[searchTableArray1 objectAtIndex:indexPath.row]];
        return cell;
    } else if (tableView == self.searchTable2) {
        BOOL nibsRegistered = NO;
        if (!nibsRegistered) {
            UINib *nib = [UINib nibWithNibName:@"LocalCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:SearchTableCellIdentifier2];
            nibsRegistered = YES;
        }
        LocalCell *cell = (LocalCell *)[tableView dequeueReusableCellWithIdentifier:SearchTableCellIdentifier2];
        [cell loadDataWithProduct:[searchTableArray2 objectAtIndex:indexPath.row]];
        return cell;
    } else if (tableView == self.searchTable3) {
        BOOL nibsRegistered = NO;
        if (!nibsRegistered) {
            UINib *nib = [UINib nibWithNibName:@"LocalCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:SearchTableCellIdentifier3];
            nibsRegistered = YES;
        }
        LocalCell *cell = (LocalCell *)[tableView dequeueReusableCellWithIdentifier:SearchTableCellIdentifier3];
        [cell loadDataWithProduct:[searchTableArray3 objectAtIndex:indexPath.row]];
        return cell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 96;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductViewController *productVC = [[ProductViewController alloc] init];
    Product *corProduct;
    
    if (tableView == self.searchTable1) {
        corProduct = [searchTableArray1 objectAtIndex:indexPath.row];
    } else if (tableView == self.searchTable2) {
        corProduct = [searchTableArray2 objectAtIndex:indexPath.row];
    } else if (tableView == self.searchTable3) {
        corProduct = [searchTableArray3 objectAtIndex:indexPath.row];
    }
    
    productVC.productID = corProduct.id;
    productVC.product = corProduct;
    [self.navigationController pushViewController:productVC animated:YES];
}

#pragma tableView dataSource&delegate End

@end
